package cn.com.easy.persistence;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;

import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import cn.com.easy.persistence.listener.BaseEntityListener;
import cn.com.easy.utils.FastJSONUtils;
import cn.com.easy.utils.JacksonJsonDateSerializer;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;

/**
 * 统一定义id的entity基类.
 * 
 * @author nibili 2014-12-21
 * 
 */
@MappedSuperclass
@EntityListeners(value = { BaseEntityListener.class })
@DynamicInsert
@DynamicUpdate
public abstract class BaseEntity implements Serializable {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 45586345374901436L;

	/** 实体主键. */
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	protected Long id; //

	/** 实体版本 */
	@Version
	protected Long version; //

	/** 创建时间. */
	protected Date createTime; //

	/** 创建人. */
	protected String createBy; //

	/** 最后修改时间. */
	protected Date lastModifyTime; //

	/** 最后修改人. */
	protected String lastModifyBy; //

	/**
	 * Gets the id.
	 * 
	 * @param <T>
	 *            the generic type
	 * @return the id
	 */
	public Long getId() {

		return id;
	}

	/**
	 * Sets the id.
	 * 
	 * @param id
	 *            the new id
	 */
	public void setId(Long id) {

		this.id = id;
	}

	/**
	 * Gets the version.
	 * 
	 * @return the version
	 */

	public Long getVersion() {

		return version;
	}

	/**
	 * Sets the version.
	 * 
	 * @param version
	 *            the new version
	 */
	public void setVersion(Long version) {

		this.version = version;
	}

	/**
	 * Gets the creates the time.
	 * 
	 * @return the creates the time
	 */
	@JsonSerialize(using = JacksonJsonDateSerializer.class)
	public Date getCreateTime() {

		return createTime;
	}

	/**
	 * Sets the creates the time.
	 * 
	 * @param createTime
	 *            the new creates the time
	 */
	public void setCreateTime(Date createTime) {

		this.createTime = createTime;
	}

	/**
	 * Gets the creates the by.
	 * 
	 * @return the creates the by
	 */
	public String getCreateBy() {

		return createBy;
	}

	/**
	 * Sets the creates the by.
	 * 
	 * @param createBy
	 *            the new creates the by
	 */
	public void setCreateBy(String createBy) {

		this.createBy = createBy;
	}

	/**
	 * Gets the last modify time.
	 * 
	 * @return the last modify time
	 */
	@JsonSerialize(using = JacksonJsonDateSerializer.class)
	public Date getLastModifyTime() {

		return lastModifyTime;
	}

	/**
	 * Sets the last modify time.
	 * 
	 * @param lastModifyTime
	 *            the new last modify time
	 */
	public void setLastModifyTime(Date lastModifyTime) {

		this.lastModifyTime = lastModifyTime;
	}

	/**
	 * Gets the last modify by.
	 * 
	 * @return the last modify by
	 */
	public String getLastModifyBy() {

		return lastModifyBy;
	}

	/**
	 * Sets the last modify by.
	 * 
	 * @param lastModifyBy
	 *            the new last modify by
	 */
	public void setLastModifyBy(String lastModifyBy) {

		this.lastModifyBy = lastModifyBy;
	}

	@Override
	public int hashCode() {

		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {

		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		BaseEntity other = (BaseEntity) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	@Override
	public String toString() {

		return FastJSONUtils.toJsonString(this);
	}

}
